#include "treemodel.h"

TreeModel::TreeModel(QObject *parent):
    QAbstractItemModel(parent)
{
    m_rootItem = new TreeItem({"A","B"});

    auto item1 = new TreeItem( { "a", "b" } );
    auto item2 = new TreeItem( { "aa", "bb" } );
    auto item3 = new TreeItem( { "aaa", "bbb" } );

    m_rootItem->appendChild(item1);
    item1->appendChild(item2);
    item2->appendChild(item3);
}

TreeModel::~TreeModel()
{
    delete m_rootItem;
}

int TreeModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
    }
    else
    {
        return m_rootItem->columnCount();
    }
}

QHash<int, QByteArray> TreeModel::roleNames() const
{
    /*
      roleName可以自己定义，我这里是随便写的
     */

    QHash<int, QByteArray> names(QAbstractItemModel::roleNames());
    names[12345] = "text";
    names[12346] = "text2";
    return names;
}

QVariant TreeModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    switch(role)
    {
    case 12345:
    {
        return static_cast<TreeItem*>(index.internalPointer())->data(0);
    }
    case 12346:
    {
        return static_cast<TreeItem*>(index.internalPointer())->data(1);
    }
    case Qt::DisplayRole:
    {
        return static_cast<TreeItem*>(index.internalPointer())->data(index.column());
    }
    default:
    {
        return QVariant();
    }
    }
}

Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
    {
        return 0;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return m_rootItem->data(section);
    }

    return QVariant();
}

QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if(!hasIndex(row, column, parent))
    {
        return QModelIndex();
    }

    TreeItem *parentItem;
    if(!parent.isValid())
    {
        parentItem = m_rootItem;
    }
    else
    {
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
    }

    TreeItem *childItem = parentItem->child(row);
    if(childItem)
    {
        return createIndex(row, column, childItem);
    }
    else
    {
        return QModelIndex();
    }
}

QModelIndex TreeModel::parent(const QModelIndex &index) const
{
    if(!index.isValid())
    {
        return QModelIndex();
    }

    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
    TreeItem *parentItem = childItem->parent();

    if(parentItem == m_rootItem)
    {
        return QModelIndex();
    }

    return createIndex(parentItem->row(), 0, parentItem);
}

int TreeModel::rowCount(const QModelIndex &parent) const
{
    TreeItem *parentItem;
    if(parent.column() > 0)
    {
        return 0;
    }

    if(!parent.isValid())
    {
        parentItem = m_rootItem;
    }
    else
    {
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
    }

    return parentItem->childCount();
}

QAbstractItemModel *TreeModel::model()
{
    return this;
}
